package cn.flying.cloud.system.service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.layui.XmSelect;
import cn.flying.cloud.base.common.vo.layui.XmSelectHelper;
import cn.flying.cloud.system.common.vo.OrgTreeTableInfo;
import cn.flying.cloud.system.org.entity.SysOrgInfo;
import cn.flying.cloud.system.org.service.SysOrgInfoService;

/**
 * <p>
 * 组织机构信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-02 13:06:35
 */
@Service
public class SysOrganizationService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysOrgInfoService sysOrgInfoService;

    /**
     * 获取组织机构tree数据
     *
     * @return
     */
    public List<OrgTreeTableInfo> getTreeTableList(SysOrgInfo orgInfo) {
        LambdaQueryWrapper<SysOrgInfo> wrapper = new LambdaQueryWrapper<>();
        if (Ft.String.isNotBlank(orgInfo.getStatus())) {
            wrapper.eq(SysOrgInfo::getStatus, orgInfo.getStatus());
        }
        wrapper.orderByAsc(SysOrgInfo::getOrgId);
        List<SysOrgInfo> list = sysOrgInfoService.list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<OrgTreeTableInfo> treeTableInfos = list.stream().map(info -> {
            OrgTreeTableInfo tableInfo = new OrgTreeTableInfo();
            BeanUtils.copyProperties(info, tableInfo);
            return tableInfo;
        }).collect(Collectors.toList());

        //从根节点开始
        String parentId = Ft.String.isNotBlank(orgInfo.getParentId()) ? orgInfo.getParentId() : "root";

        return setChildren(treeTableInfos, parentId);
    }

    private List<OrgTreeTableInfo> setChildren(List<OrgTreeTableInfo> list, String parentId) {
        return list.stream()
                // 过滤父节点
                .filter(parent -> parentId.equals(parent.getParentId()))
                // 递归赋值children
                .peek(child -> {
                    List<OrgTreeTableInfo> temp = setChildren(list, child.getOrgId());
                    if (CollectionUtils.isEmpty(temp)) {
                        child.setChildren(new ArrayList<>());
                        child.setParent(false);
                    } else {
                        child.setChildren(temp);
                        child.setParent(true);
                    }
                })
                .collect(Collectors.toList());
    }

    /**
     * 组织结构下拉
     *
     * @return
     */
    public List<XmSelect> getSelectList() {
        LambdaQueryWrapper<SysOrgInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(SysOrgInfo::getOrgId);
        List<SysOrgInfo> list = sysOrgInfoService.list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<XmSelect> xmSelects = list.stream().map(info -> {
            XmSelect xmSelect = new XmSelect();
            xmSelect.setValue(info.getOrgId());
            xmSelect.setName(info.getOrgName());
            xmSelect.setPid(info.getParentId());
            xmSelect.setDisabled(false);
            xmSelect.setSelected(false);
            return xmSelect;
        }).collect(Collectors.toList());

        return XmSelectHelper.setChild(xmSelects, "root");
    }

}
